package com.easyfun.healthmagicbox.comm;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.UserJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.UserToPersonsListJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.pojo.User;
import com.easyfun.healthmagicbox.pojo.UserToPersons;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;

public class UserLoginTask extends BaseCommTask implements Runnable,
		DatabaseAccess {
	String username;
	String password;

	public UserLoginTask(String username, String password, Context context,
			CommCallback callback) {

		super(context,callback);
		this.username = username;
		this.password = password;
	}

	@Override
	public DatabaseHelper getHelper(Context context) {

		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}

	@Override
	public void run() {

		JSONObject serializedObj = new JSONObject();
		String result = "";
		try {
			serializedObj.put("username", username);
			serializedObj.put("password", password);

			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", serializedObj.toString());
			result = super.uploadByHTTP(parmas, ConstantData.UserLoginURI);
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.FAILED) {
				ccb.setCommResult(this, result,
						ServerResponseCode.FAILED);
				return;
			}
			
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.SUCCESS) {
				ccb.setCommResult(this, result,
						ServerResponseCode.SUCCESS);
				return;
			} 
			if ((result.length() > 0)) {
				 {
					Dao<User, Integer> user = getHelper(mcontext)
							.getUserDao();
					User data =  
							(User) UserJsonWrapper
							.getInstance(result,UserJsonWrapper.class).getDataPojo();

					DeleteBuilder<User, Integer> deleteBuilder =

					user.deleteBuilder();
					// only delete the rows where password is null
					deleteBuilder.where().eq("username", username);
					deleteBuilder.delete();
					//user plain password not MD5 encrypted
					data.setPassword(password);
					user.create(data);

					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();			
			ccb.setCommResult(this, result, ServerResponseCode.NOCONNECTION);			
		}
	}
}
